package com.ndp.fb.fbagent.service.webrequest;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.fb.config.ConfigCentre;
import com.ndp.fb.constant.ConfigConst;
import com.ndp.fb.fbagent.service.help.CheckAccessTokenHelper;
import com.ndp.fb.rdb.api.FbUserRdbService;
import com.restfb.BinaryAttachment;
import com.restfb.DefaultWebRequestor;
import com.restfb.WebRequestor;
import com.restfb.exception.FacebookJsonMappingException;
import com.restfb.json.JsonException;
import com.restfb.json.JsonObject;
import com.restfb.util.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Created by infi.he on 2015/5/5.
 */
@Component
@Scope(value = "prototype")
public class SyncNoAccountQueueWebRequestor extends DefaultWebRequestor {
    private static final org.slf4j.Logger log = LoggerFactory.getLogger("com.ndpmedia.pmd.fb.apilimit");
    private static final org.slf4j.Logger loge = LoggerFactory.getLogger("com.ndpmedia.pmd.fb.apilimitexception");
    private static final org.slf4j.Logger logx = LoggerFactory.getLogger(SyncNoAccountQueueWebRequestor.class);

    @Reference
    FbUserRdbService fbUserRdbService;
    @Autowired
    CheckAccessTokenHelper checkAccessTokenHelper;

    @Override
    public Response executeGet(String url) throws IOException {
        if (ConfigCentre.getBoolean(ConfigConst.FACEBOOK_REQEUST_IS_MOCK)) {
            logx.debug("ismock >>>>>>>> " + true);
            url = url.replace("https://", "http://");
        }
        logx.info(">>>getAccountFromUrl:" + url);

        String result = checkAccessTokenHelper.checkAccessToken(url);
        if (!StringUtils.isBlank(result)) {
            Response response = new WebRequestor.Response(200, result);
            return response;
        }

        Response response = super.executeGet(url);
        return checkAccessTokenHelper.checkFBResponse(response, url, null);
    }

    @Override
    public Response executePost(String url, String parameters, BinaryAttachment... binaryAttachments) throws IOException {
        if (ConfigCentre.getBoolean(ConfigConst.FACEBOOK_REQEUST_IS_MOCK)) {
            logx.debug("ismock >>>>>>>> " + true);
            url = url.replace("https://", "http://");
        }
        logx.info(">>>getAccountFromUrl:" + url);

        String result = checkAccessTokenHelper.checkAccessToken(url,parameters);
        if (!StringUtils.isBlank(result)) {
            Response response = new WebRequestor.Response(200, result);
            return response;
        }
        Response response = super.executePost(url, parameters, binaryAttachments);
        return checkAccessTokenHelper.checkFBResponse(response, url, parameters);
    }

    protected void throwFacebookResponseLimitExceptionIfNecessary(String url, Long time, String json, Integer httpStatusCode) {
        try {
            if (json.startsWith("{")) {
                int e = Math.min(50, json.length());
                if (json.substring(0, e).contains("\"error\"")) {
                    JsonObject errorObject = new JsonObject(json);
                    if (errorObject != null && errorObject.has("error")) {
                        JsonObject innerErrorObject = errorObject.getJsonObject("error");
                        Integer errorCode = innerErrorObject.has("code") ? StringUtils.toInteger(innerErrorObject.getString("code")) : null;
                        Integer errorSubcode = innerErrorObject.has("error_subcode") ? StringUtils.toInteger(innerErrorObject.getString("error_subcode")) : null;
                        loge.debug("time-_-" + time + "-_-Exception-_-errorCode-_-" + errorCode + "-_-errorSubcode-_-" + errorSubcode + "-_-url-_-" + url);
                    }
                }
            }
        } catch (JsonException var10) {
            throw new FacebookJsonMappingException("Unable to process the Facebook API response", var10);
        }
    }


}
